package ao

import (
	"context"
	"log"
	"micro.user.auth.server/utils"
	"time"

	"micro.user.auth.server/config"
	"micro.user.auth.server/impl"

	compb "gitee.com/micro-services/protocol/common"
	pb "gitee.com/micro-services/protocol/user/server"
)

func GenerateToken(ctx context.Context, r *pb.GenerateTokenReq, w *pb.GenerateTokenRsp) error {
	factory := impl.BuilderGenerateToken()
	var tokenKey = &pb.ResolveToken{}
	key := make(map[string]interface{})
	now := time.Now().Unix()
	switch r.Platform {
	case compb.UserPlatFormType_ADMIN_USER:
		tokenKey.UserID = r.UserID
		tokenKey.UserName = r.UserName
		tokenKey.Phone = r.Phone
		tokenKey.HotelID = r.HotelID
		tokenKey.Email = r.Email
		tokenKey.TimeNow = now
		tokenKey.Expiration = time.Now().Add(time.Hour * time.Duration(config.SecretHours)).Unix()
		tokenKey.Platform = compb.UserPlatFormType_ADMIN_USER

		key = utils.StructJsonToMap(tokenKey)
	case compb.UserPlatFormType_MINI_PROGRAM_USER:
		tokenKey.UserID = r.UserID
		tokenKey.UserName = r.UserName
		tokenKey.Expiration = time.Now().Add(time.Hour * time.Duration(config.SecretHours)).Unix()
		tokenKey.Platform = compb.UserPlatFormType_ADMIN_USER

		key = utils.StructJsonToMap(tokenKey)
	case compb.UserPlatFormType_GENERAL_USER:
		tokenKey.UserID = r.UserID
		tokenKey.UserName = r.UserName
		tokenKey.Phone = r.Phone
		tokenKey.Email = r.Email
		tokenKey.Expiration = time.Now().Add(time.Hour * time.Duration(config.SecretHours)).Unix()
		tokenKey.Platform = compb.UserPlatFormType_ADMIN_USER

		key = utils.StructJsonToMap(tokenKey)
	default:
	}

	rsp, err := factory.GenerateToken(r.UserID, r.Platform.String(), key)
	if err != nil {
		return err
	}

	w.Token = rsp.Token
	w.Expiration = time.Unix(time.Now().Add(time.Hour*time.Duration(config.SecretHours)).Unix(), 0).Format("2006-01-02 15:04:05")
	log.Printf("rsp:%+v", w)
	return nil
}

func CheckToken(ctx context.Context, r *pb.CheckTokenReq, w *pb.CheckTokenRsp) error {
	factory := impl.BuilderGenerateToken()
	b, err := factory.CheckToken(1, r.Token)
	if err != nil {
		return err
	}

	w.IsValid = b
	log.Printf("w:%+v", w)
	return nil
}

func ResolveToken(ctx context.Context, r *pb.ResolveTokenReq, w *pb.ResolveTokenRsp) error {
	factory := impl.BuilderGenerateToken()
	rsp, err := factory.ResolveToken(1, r.Token)
	if err != nil {
		return err
	}

	claims, err := utils.MapJsonToStruct(rsp)
	if err != nil {
		return err
	}

	w.Claims = claims
	log.Printf("w:%+v", w)
	return nil
}

func CheckUserRole(ctx context.Context, r *pb.CheckUserRoleReq, w *pb.CheckUserRoleRsp) error {
	panic("implement me")
}

func GetWxUserInfo(ctx context.Context, r *pb.GetWxUserInfoReq, w *pb.GetWxUserInfoRsp) error {
	factory := impl.BuilderWxUser()
	rsp, err := factory.GetWxUserInfo(r.Code)
	if err != nil {
		return err
	}

	w.UnionId = rsp.UnionId
	w.SessionKey = rsp.SessionKey
	w.OpenId = rsp.OpenId
	log.Printf("w:%+v", w)
	return nil
}

func GetWxToken(ctx context.Context, r *pb.GetWxTokenReq, w *pb.GetWxTokenRsp) error {
	factory := impl.BuilderWxUser()
	rsp, err := factory.GetWxUserToken(compb.UserPlatFormType_MINI_PROGRAM_USER.String(), r.WxUserID)
	if err != nil {
		return err
	}

	w.Token = rsp
	return nil
}
